package com.cqhilink.api.common.utils;


import java.util.List;

/**
 * levelcode 相关工具类
 * Created by yangcs on 2016/7/8.
 */
public class LevelcodeUtil {

    public static String getLevelcodeByParent(String parentLevelcode, List<String> sunLevelCodes) throws Exception {
        String maxlevelcode = "";
        String sunlevelcode = "";
        if (parentLevelcode.equals("000")) {
            parentLevelcode = "";
        }
        if (sunLevelCodes != null && sunLevelCodes.size() > 0) {
            maxlevelcode = getMaxOfList(sunLevelCodes);
            if (maxlevelcode.equals("")) {
                sunlevelcode = parentLevelcode + "001";
            } else {
                String code = "";
                String newCode = "";
                if (maxlevelcode.length() > 3) {
                    code = maxlevelcode.substring(maxlevelcode.length() - 3);
                } else {
                    code = maxlevelcode;
                }
                //可能出现异常：往上层抛异常
                int i = Integer.parseInt(code);
                i++;
                if (i > 0 && i < 10) {
                    newCode = "00" + i;
                } else if (i >= 10 && i < 100) {
                    newCode = "0" + i;
                } else if (i >= 100 && i < 1000) {
                    newCode = "" + i;
                } else if (i >= 1000) {
                    throw new Exception("下级节点超过999个，不符合规则！");
                }
                sunlevelcode = parentLevelcode + newCode;
            }
        } else {
            sunlevelcode = parentLevelcode + "001";
        }
        return sunlevelcode;
    }

    /**
     * 根据父亲节点层级码和本级最大层级码，生成本级新增节点层级码
     * 适用于组织下新增组织，组织下新增区域（父亲节点设置为""），区域下新增区域
     *
     * @param maxlevelcode
     * @param maxlevelcode
     * @return
     * @throws Exception
     */
    public static String getSunlevelcode(String parentlevelcode, String maxlevelcode) throws Exception {
        String sunlevelcode = "";

        if (maxlevelcode.equals("")) {
            //当前节点下级没有子节点
            sunlevelcode = parentlevelcode + "001";
        } else {
            String three = "";
            String newThree = "";

            //取字符串末尾三个字符
            if (maxlevelcode.length() > 3) {
                // String java.lang.String.substring(int beginIndex):
                // IndexOutOfBoundsException - if beginIndex is negative or larger than the length of this String object
                three = maxlevelcode.substring(maxlevelcode.length() - 3);
            } else {
                three = maxlevelcode;
            }

            //可能出现异常：往上层抛异常
            int i = Integer.parseInt(three);
            i++;

            if (i > 0 && i < 10) {
                newThree = "00" + i;
            } else if (i >= 10 && i < 100) {
                newThree = "0" + i;
            } else if (i >= 100 && i < 1000) {
                newThree = "" + i;
            } else if (i >= 1000) {
                throw new Exception("该节点的下级节点超过999个，不符合规则！");
            }

            if (maxlevelcode.length() > 3) {
                sunlevelcode = parentlevelcode + newThree;
            } else {
                sunlevelcode = newThree;
            }
        }

        return sunlevelcode;
    }

    /**
     * 比较Lis中元素的字典顺序，得到字典顺序最靠后的元素
     *
     * @param list
     * @return String 不会返回null
     */
    private static String getMaxOfList(List<String> list) {
        String max = "";
        for (String str : list) {
            if (str != null) {
                //必须判断非null，否则报java.lang.NullPointerException
                if (str.compareTo(max) > 0) {
                    max = str;
                }
            }
        }
        return max;
    }
}
